﻿#include "QButtonBase.h"

QCOMMWIDGETS_BEGIN_NAMESPACE


QGroupButton::QGroupButton(const QString &strIcon, const QString &strText, EOrientation orientation, QWidget *parent)
    : QToolButton(parent), m_bPressed(false)
{
    initUI(QIcon(strIcon), strText, orientation);

    this->setFocusPolicy(Qt::NoFocus);

    connect(this, SIGNAL(signClicked()), this, SLOT(slotClicked()));
}

QGroupButton::QGroupButton(const QIcon &icon, const QString &strText, EOrientation orientation, QWidget *parent)
    : QToolButton(parent), m_bPressed(false)
{
    initUI(icon, strText, orientation);

    this->setFocusPolicy(Qt::NoFocus);

    connect(this, SIGNAL(signClicked()), this, SLOT(slotClicked()));
}

QGroupButton::QGroupButton(const QString &strIcon, QWidget *parent)
    : QToolButton(parent), m_bPressed(false)
{
    initUI(QIcon(strIcon), QString(""), EOrientation::e_onlyIcon);

    this->setFocusPolicy(Qt::NoFocus);
	this->setBackgroundRole(QPalette::NoRole);//设置背景透明
	//this->setFlat(true);//设置透明
    //connect(this, SIGNAL(signClicked()), this, SLOT(slotClicked()));
}

QGroupButton::QGroupButton(const QIcon &icon, const QString &strText,QFont font, QColor color, EOrientation orientation, QWidget *parent)
{
    initUI(icon, strText, orientation);

    this->setFocusPolicy(Qt::NoFocus);
    QPalette pe;
    pe.setColor(QPalette::WindowText, color);
    this->setPalette(pe);
    this->setFont(font);
    this->setBackgroundRole(QPalette::NoRole);//设置背景透明
    connect(this, SIGNAL(signClicked()), this, SLOT(slotClicked()));
}

QGroupButton::~QGroupButton()
{

}

void QGroupButton::initUI(const QIcon &icon, const QString &strText, EOrientation orientation)
{
    switch (orientation) {
    case e_onlyIcon:
        this->setToolButtonStyle(Qt::ToolButtonIconOnly);
        this->setIcon(icon);
        break;
    case e_onlyText:
        this->setToolButtonStyle(Qt::ToolButtonTextOnly);
        this->setText(strText);
        break;
    case e_textUnderIcon:
        this->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        this->setIcon(icon);
        this->setText(strText);
        break;
    case e_textBesideIcon:
    default:
        this->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        this->setIcon(icon);
        this->setText(strText);
        break;
    }
}

void QGroupButton::setButtonHeight(int height)
{
    this->setFixedHeight(height);
    int nIconSize = (height / 3) * 2;
    this->setIconSize(QSize(nIconSize, nIconSize));
}

void QGroupButton::mousePressEvent(QMouseEvent *e)
{
    if (hitButton(e->pos())){  // 按下时候光标在按钮区域
        m_bPressed = true;
    }
    QToolButton::mousePressEvent(e);   // 继续默认处理
}

void QGroupButton::mouseReleaseEvent(QMouseEvent *e)
{
    if (m_bPressed && hitButton(e->pos())){  // 松开时候光标仍在按钮区域
        emit signClicked();
    }
    QToolButton::mouseReleaseEvent(e);   // 继续默认处理

    m_bPressed = false;
}

QCOMMWIDGETS_END_NAMESPACE
